<template>


  <el-dialog title="修改密码" :visible="visible"  @close="visible = false">
    <el-form :model="form" ref="form" status-icon :rules="rules" label-width="100px">
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="form.password"/>
      </el-form-item>
      <el-form-item label="确认密码" prop="repassword">
        <el-input type="password" v-model="form.repassword"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="loading" @click="onOk">确 定</el-button>
    </div>
  </el-dialog>

</template>

<script>
  import axios from "@/util/axios";
  import md5 from "md5";

  const form = () => {
    return {
      password:'',
    }
  }

  export default {


    data(){
      return {
        visible:false,
        loading:false,
        form:form(),
        repassword:'',
        parentName:'',
        rules:{
          password:[{
            required: true, message: '请输入密码', trigger: 'change'
          },{
            trigger:'change', min: 5, message: '最少5个字符'
          }],
          repassword:[{
            required: true, trigger: 'change',
            validator: (rule,value,callback) => {
              if(value != this.form.password){
                callback(new Error('两次输入不一致'));
              }else{
                callback();
              }
            }
          }]
        }
      };

    },

    methods: {
      onOk(){

        this.loading = true;
        const {userId,password} = this.form;
        axios.patch('/api/v1/suser/pwd',axios.form({userId,password:md5(password)}))
        .then(()=>{
          this.$message.success('修改成功');
          this.visible = false;
          this.loading = false;
          this.$emit('success');
        })
        .catch( e => {
          setTimeout( e => this.loading = false,2000);
        });

      },

      edit(id){
        this.visible = true;
        this.form = form();
        this.form.userId = id;
        this.$refs.form && this.$refs.form.resetFields();
      }

    }
  }
</script>

